package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.entity.activity.ghost.GhostBossData;
import com.yanqu.road.entity.activity.ghost.GhostSimplePlayer;
import com.yanqu.road.logic.pb.GhostPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostActivity;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.List;

@Cmd(code = CrossProtocol.C2_GHOST_ACTIVITY_ENTER_BOSS_ROOM, desc = "跨服进入boss房间（获取boss数据）")
public class CrossGhostEnterBossCmd extends CrossCommand {

    int activityId;
    GhostActivityProto.GhostSimplePlayerMsg playerData;
    int bossId;
    long userResetTime;

    GhostBossData bossData;
    List<GhostSimplePlayer> playerList;

    GhostActivity activity;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.CrossEnterBossRoomReqMsg reqMsg = GhostActivityProto.CrossEnterBossRoomReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        playerData = reqMsg.getPlayerData();
        bossId = reqMsg.getBossId();
        userResetTime = reqMsg.getUserResetTime();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        activity = GhostMgr.getActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        synchronized (activity.getLockObject()) {
            // 请求校验
            ret = activity.checkGhostRequest(userResetTime, true);
            if (ret != 0) {
                return;
            }

            bossData = activity.getGhostBossData(-1, bossId);
            if (bossData == null) {
                ret = GameErrorCode.E_GHOST_ACTIVITY_CAN_NOT_FIND_BOSS;
                return;
            }

            if (bossData.getCurHp() <= 0) {
                ret = GameErrorCode.E_GHOST_ACTIVITY_BOSS_DEAD;
                return;
            }

            // 进入房间
            playerList = activity.enterRoom(bossId, GhostPb.parseGhostSimplePlayer(playerData));

        }

    }

    @Override
    public void sendData() {
        GhostActivityProto.EnterBossRoomRespMsg.Builder builder = GhostActivityProto.EnterBossRoomRespMsg.newBuilder();
        builder.setRet(ret);
        if (bossData != null) {
            builder.setBossData(GhostPb.parseGhostBossDataMsg(bossData));
        }
        if (ret == 0) {
            for (GhostSimplePlayer simplePlayer : playerList) {
                builder.addPlayerMsgList(GhostPb.parseGhostSimplePlayerMsg(simplePlayer));
            }
        }
        MessageHelper.sendPacket(serverId, playerData.getUserId(), YanQuMessageUtils.buildMessage(ClientProtocol.U_GHOST_ENTER_BOSS_ROOM, builder));

        // 返回之后再判断同步
        if (activity != null && activity.needAdvanceSyncAction()) {
            activity.syncGhostPlayerAction(true);
        }
    }
}
